#!/usr/bin/env Rscript
################################################################################
# @ Author       : Pengdong Yan
# @ Email        : yanpd01@snnu.edu.cn
# @ Encoding     : UTF-8
# @ Language     :
# @ Date         : 2022-03-21 16:35
# @ LastEditTime : 2022-04-07 00:48
# @ Description  :
################################################################################

library(yyeasy)
library(ggpmisc)
library(extrafont)
fonts()


rm(list = ls())
a0 <- yyread("0_data/all_standard_curve.xlsx", excel = T)
a1 <- filter(a0, material == "磷酸盐")
lm1 <- lm(y ~ x, a1)
cat(
    paste0(
        "y=", coef(lm1)[2], "x+", coef(lm1)[1], "\n",
        "R2=", summary(lm1)$adj.r.squared
    )
)
resid(lm1)
predict(lm1)

a2 <- yyread(clipboard(), header = F)
a3 <- (a2$y - coef(lm1)[1]) / coef(lm1)[2]
head(a3)
# yywrite(cbind(a2, a3), "tmp.tsv")
a0 %>%
    filter(material == "乌头碱") %>%
    ggplot(aes(x, y)) +
    geom_point(color = "grey25") +
    labs(x = "浓度", y = "响应面积") +
    theme(axis.title = element_text(family = "SimSun", face = "bold"))



a0 <- yyread("0_data/all_standard_curve.xlsx", excel = T)
p_b_swj <-
    a0 %>%
    filter(type == "生物碱") %>%
    ggplot(aes(x, y)) +
    facet_wrap(~material) +
    geom_point(color = "grey25") +
    geom_smooth(method = "lm", formula = y ~ x, color = "grey30") +
    stat_poly_eq(aes(label = ..eq.label..), label.y = 0.98, label.x = 0.02, family = "serif", size = 10 / .pt) +
    stat_poly_eq(aes(label = ..adj.rr.label..), rr.digits = 4, label.y = 0.84, label.x = 0.02, family = "serif", size = 10 / .pt) +
    stat_poly_eq(aes(label = ..p.value.label..), p.digits = 4, label.y = 0.70, label.x = 0.02, family = "serif", size = 10 / .pt) +
    scale_y_continuous(labels = scales::scientific) +
    guides(color = "none") +
    labs(x = expression(paste("质量（", mu, "g）")), y = "响应面积") +
    theme_bw2(10.5) +
    theme(
        strip.text = element_text(size = 10.5, family = "SimSun"),
        axis.title = element_text(family = "SimSun"),
        axis.text = element_text(family = "serif")
    )
yysave(p_b_swj, "p_b_swj.svg", 18, 12)
yysave(p_b_swj, "p_b_swj.png", 18, 12, dpi = 1200)


tmp_soil <- a0 %>% filter(type == "土壤")
tmp_soil$material <- factor(tmp_soil$material, levels = c("氨态氮", "硝态氮", "磷酸盐"))
p_b_soil <-
    tmp_soil %>%
    ggplot(aes(x, y)) +
    facet_wrap(~material, scales = "free") +
    geom_point(color = "grey25") +
    geom_smooth(method = "lm", formula = y ~ x, color = "grey30") +
    stat_poly_eq(aes(label = ..eq.label..), label.y = 0.98, label.x = 0.02, family = "serif", size = 10 / .pt) +
    stat_poly_eq(aes(label = ..adj.rr.label..), rr.digits = 4, label.y = 0.84, label.x = 0.02, family = "serif", size = 10 / .pt) +
    stat_poly_eq(aes(label = ..p.value.label..), p.digits = 4, label.y = 0.70, label.x = 0.02, family = "serif", size = 10 / .pt) +
    # scale_y_continuous(labels = scales::scientific) +
    guides(color = "none") +
    labs(x = "浓度（mg/L）", y = "响应值") +
    theme_bw2(10.5) +
    theme(
        strip.text = element_text(size = 10.5, family = "SimSun"),
        axis.title = element_text(family = "SimSun"),
        axis.text = element_text(family = "serif")
    )
yysave(p_b_soil, "p_b_soil.svg", 18, 6)
yysave(p_b_soil, "p_b_soil.png", 18, 6, dpi = 1200)

tmp_po <- a0 %>% filter(type == "成分")
tmp_po$material <- factor(tmp_po$material, levels = c("多糖", "可溶性蛋白"))
p_b_po <-
    tmp_po %>%
    ggplot(aes(x, y)) +
    facet_wrap(~material, scales = "free") +
    geom_point(color = "grey25") +
    geom_smooth(method = "lm", formula = y ~ x, color = "grey30") +
    stat_poly_eq(aes(label = ..eq.label..), label.y = 0.98, label.x = 0.02, family = "serif", size = 10 / .pt) +
    stat_poly_eq(aes(label = ..adj.rr.label..), rr.digits = 4, label.y = 0.84, label.x = 0.02, family = "serif", size = 10 / .pt) +
    stat_poly_eq(aes(label = ..p.value.label..), p.digits = 4, label.y = 0.70, label.x = 0.02, family = "serif", size = 10 / .pt) +
    # scale_y_continuous(labels = scales::scientific) +
    guides(color = "none") +
    labs(x = "浓度（mg/mL）", y = "响应值") +
    theme_bw2(10.5) +
    theme(
        strip.text = element_text(size = 10.5, family = "SimSun"),
        axis.title = element_text(family = "SimSun"),
        axis.text = element_text(family = "serif")
    )
yysave(p_b_po, "p_b_po.svg", 13, 6)
yysave(p_b_po, "p_b_po.png", 13, 6, dpi = 1200)


## tmp
library(yyeasy)
a1 <- yyread(clipboard(), header = F); lm1 <- lm(V2~V1, a1); summary(lm1); plot(a1$V1, a1$V2); abline(lm1)
cat(
    paste0(
        "y=", round(coef(lm1)[2], 3), "x+", round(coef(lm1)[1], 3), "\n",
        "R2=", round(summary(lm1)$adj.r.squared, 4)
    )
)
